﻿Imports System.Data.OleDb


Partial Public Class mobilestandards
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack() Then
            displayTable(Request.Params("sort"), Request.Params("tag"))
        Else
            displayTable(Request.Params("sort"), TagChooser.Text)
        End If



    End Sub

    Private Sub displayTable(ByVal sortby As String, Optional ByVal tagChosen As String = "")
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("mobilestandardsdb").ConnectionString)


        oleDbConn.Open()
        Dim cmdQuery As String



        Select Case sortby
            Case "Title"
                cmdQuery = "SELECT DISTINCT ID,Title,Author,Number,Abstract,Hyperlink,[User] FROM papersweeksandtags ORDER BY " & sortby
            Case "Author"
                cmdQuery = "SELECT DISTINCT ID,Title,Author,Number,Abstract,Hyperlink,[User] FROM papersweeksandtags ORDER BY " & sortby
            Case "Number"
                cmdQuery = "SELECT DISTINCT ID,Title,Author,Number,Abstract,Hyperlink,[User] FROM papersweeksandtags ORDER BY " & sortby
            Case Else
                cmdQuery = "SELECT DISTINCT ID,Title,Author,Number,Abstract,Hyperlink,[User] FROM papersweeksandtags"

        End Select


        If tagChosen <> "" Then
            cmdQuery = cmdQuery.Replace("papersweeksandtags", "papersweeksandtags WHERE Tag=@TagChosen")
        End If


        Dim dbcomm = New OleDbCommand(cmdQuery, oleDbConn)

        If tagChosen <> "" Then
            dbcomm.Parameters.AddWithValue("@TagChosen", tagChosen)
        End If

        Dim dbread = dbcomm.ExecuteReader()

        Dim t As New Table

        t.Attributes.Add("style", "width:100%")


        Dim thr As New TableHeaderRow


        thr.Controls.Add(getHeaderCellWithText("Author"))
        thr.Controls.Add(getHeaderCellWithText("Title"))
        thr.Controls.Add(getHeaderCellWithText("Week"))
        thr.Controls.Add(getHeaderCellWithText("User"))

        t.Controls.Add(thr)


        While dbread.Read()
            Dim tr As New TableRow()

            tr.Controls.Add(getCellWithText(dbread.Item("Author")))
            tr.Controls.Add(getCellWithText(dbread.Item("Title"), dbread.Item("Hyperlink"), dbread.Item("ID")))
            tr.Controls.Add(getCellWithText(dbread.Item("Number")))
            tr.Controls.Add(getCellWithText(dbread.Item("User")))

            t.Controls.Add(tr)

            Dim tr2 As New TableRow

            tr2.Controls.Add(getWideCellWithText(dbread.Item("Abstract")))

            t.Controls.Add(tr2)

        End While

        PlaceHolder1.Controls.Add(t)


    End Sub

    Private Function getCellWithText(ByVal cellStr As String)
        Dim mycell As New TableCell
        mycell.Text = cellStr
        mycell.Attributes.Add("class", "details")

        Return mycell
    End Function
    Private Function getCellWithText(ByVal cellStr As String, ByVal urlAddress As String)
        Dim mycell As New TableCell
        'mycell.Text = cellStr
        'mycell.Controls.Add(New LiteralControl("<B>" & cellStr & "</B>"))
        Dim hl As New HyperLink()
        hl.NavigateUrl = urlAddress
        hl.Text = cellStr
        hl.Attributes.Add("class", "nav")
        mycell.Controls.Add(hl)
        Return mycell
    End Function

    Private Function getCellWithText(ByVal cellStr As String, ByVal urlAddress As String, ByVal paperID As Integer)
        Dim mycell As TableCell = getCellWithText(cellStr, urlAddress)
        mycell.Controls.Add(New LiteralControl("|&nbsp;&nbsp;"))
        Dim hl As New HyperLink
        hl.Text = "Discuss"
        hl.NavigateUrl = "discussion.aspx?ID=" & paperID
        mycell.Controls.Add(hl)
        mycell.Controls.Add(New LiteralControl("|&nbsp;&nbsp;"))
        Dim ratingsHyperlink As New HyperLink
        ratingsHyperlink.Text = "Rate"
        ratingsHyperlink.NavigateUrl = "Rate.aspx?ID=" & paperID
        mycell.Controls.Add(ratingsHyperlink)

        Return mycell

    End Function




    Private Function getWideCellWithText(ByVal cellStr As String)
        Dim mycell As New TableCell
        mycell.ColumnSpan = 4
        mycell.Text = cellStr
        mycell.Attributes.Add("class", "abstract")

        Return mycell
    End Function



    Private Function getHeaderCellWithText(ByVal cellStr As String)
        Dim mycell As New TableHeaderCell
        Dim hl As New HyperLink
        hl.Text = cellStr
        If (cellStr = "Week") Then cellStr = "Number"
        hl.NavigateUrl = "mobilestandards.aspx?sort=" & cellStr
        hl.Attributes.Add("class", "sort")
        mycell.Controls.Add(hl)
        Return mycell

    End Function



End Class